//
//  ILGAnimationDisplayVC.m
//  ILGDemo
//
//  Created by zhangtongle-Pro on 2017/10/23.
//  Copyright © 2017年 Happy. All rights reserved.
//

#import "ILGAnimationDisplayVC.h"
#import "ILGHeartFlyView.h"
#import "ILGTestView.h"

@interface ILGAnimationDisplayVC ()
{

}

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *maskNavigationBar;
@end

@implementation ILGAnimationDisplayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSString *methodName = [NSString stringWithFormat:@"performAnimation%d", self.animationType];
    SEL selector = NSSelectorFromString(methodName);
    if ([self respondsToSelector:selector]) {
//        [self performSelector:selector withObject:nil];
    }
    
    [self testAnimation];
}

- (void)dealloc {
    [_maskNavigationBar removeFromSuperview];
}

- (void)testAnimation
{
    [self drawMLine];
}


#pragma mark - UI

- (void)createUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(20, 450, 50, 40)];
    [button setTitle:@"复原" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(resetButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    ILGTestView *maskNavigationBar = [[ILGTestView alloc] initWithFrame:CGRectMake(0, 0, self.view.ilg_width, 44)];
    maskNavigationBar.backgroundColor = [UIColor blackColor];
    [UIApplication.sharedApplication.keyWindow addSubview:maskNavigationBar]; //会被强引用，如果不removeFromSuperView，则不会释放
    self.maskNavigationBar = maskNavigationBar;
}


#pragma mark - Animation

- (void)performAnimation0
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    [UIView animateWithDuration:.5 animations:^{
       view.layer.anchorPoint = CGPointMake(0, 0);
    }];
}

//画M曲线
- (void)drawMLine {
    UIImageView *animationViwe = [[UIImageView alloc] initWithFrame:CGRectMake(74, 74, 44, 44)];
    animationViwe.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationViwe];

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 74, 111);
    CGPathAddCurveToPoint(path, NULL, 74, 300, 160, 300, 160, 111);
    CGPathAddCurveToPoint(path, NULL, 160, 300, 246, 300, 246, 111);
    
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.path = path;
    keyAnimation.duration = 5.0;
    keyAnimation.rotationMode = @"auto"; // 方块随着曲线自动调整角度，以保证与轨迹线平行
    CGPathRelease(path);

    [animationViwe.layer addAnimation:keyAnimation forKey:@"position"];
    
    animationViwe.frame = CGRectMake(209, 74, 74, 74);
}

- (void)animationForRotation
{
    UIImageView *animationViwe = [[UIImageView alloc] initWithFrame:CGRectMake(74, 74, 44, 44)];
    animationViwe.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationViwe];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.toValue = @(M_PI);
    animation.autoreverses = YES;
    animation.duration = 1.0;
    
    [animationViwe.layer addAnimation:animation forKey:@"rotation"];
}

- (void)performAnimation1
{
    
}

- (void)performAnimation2
{
    UIImageView *animationViwe = [[UIImageView alloc] initWithFrame:CGRectMake(74, 74, 44, 44)];
    animationViwe.backgroundColor = [UIColor redColor];
    [self.view addSubview:animationViwe];
    animationViwe.layer.anchorPoint = CGPointMake(0.0, 1);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.fromValue = @(1.0);
    animation.toValue = @(2.0);
    animation.duration = 0.8;
    animation.repeatCount = 1;
    animation.autoreverses = YES;
    
    
    [animationViwe.layer addAnimation:animation forKey:@"animation1"];
}

- (void)performAnimation3
{
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    contentView.backgroundColor = [UIColor lightGrayColor];
    _contentView = contentView;
    [self.view addSubview:contentView];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flyHesart)];
    [self.view addGestureRecognizer:tapGes];
}
- (void)flyHesart
{
    ILGHeartFlyView *flyView = [[ILGHeartFlyView alloc] initWithFrame:CGRectMake(_contentView.frame.size.width/2.0, _contentView.frame.size.height, 44, 44)];
    [flyView animateInView:_contentView];
}

- (void)performAnimation4
{
    [self.view.layer addSublayer:[self animationLayer]];
}

- (void)performAnimation5
{
    
}

- (void)performAnimation6 {
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 160, 120)];
//    contentView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:contentView];
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(112, 80)];
    [path addArcWithCenter:CGPointMake(112, 72) radius:8 startAngle:M_PI*0.5 endAngle:M_PI*0 clockwise:NO];
    [path addLineToPoint:CGPointMake(120, 8)];
    [path addArcWithCenter:CGPointMake(112, 8) radius:8 startAngle:M_PI*2 endAngle:M_PI*1.5 clockwise:NO];

    [path addLineToPoint:CGPointMake(8, 0)];
    [path addArcWithCenter:CGPointMake(8, 8) radius:8 startAngle:M_PI*1.5 endAngle:M_PI*1 clockwise:NO];

    [path addLineToPoint:CGPointMake(0, 80)];
    path.lineCapStyle = kCGLineCapSquare;

//    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
//    shapeLayer.path = path.CGPath;
//    shapeLayer.fillColor = [UIColor redColor].CGColor;
////    shapeLayer.frame = contentView.bounds;
//    contentView.layer.mask = shapeLayer;

    CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
    borderLayer.path = path.CGPath;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = [UIColor purpleColor].CGColor;
    borderLayer.lineWidth = 2.0;
    borderLayer.strokeStart = 0;
    borderLayer.strokeEnd = 1.0;
//    borderLayer.frame = contentView.bounds;
    [contentView.layer addSublayer:borderLayer];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @(0.0);
    animation.toValue = @(1.0f);
    animation.duration = 2.5f;
    animation.removedOnCompletion = NO;
    animation.fillMode  = kCAFillModeForwards;
    [borderLayer addAnimation:animation forKey:@"strokeEnd"];
}


#pragma mark - event

- (void)resetButtonClicked:(UIButton *)button
{

}


#pragma mark - other
/**
 仿音频波动动画，无法暂停

 @return layer
 */
- (CALayer *)animationLayer {
    CALayer *layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(50, 100, 180, 27);
    
    UIColor *tintColor = [UIColor redColor];
    CGSize lineSize = CGSizeMake(3, layer.frame.size.height);
    CGFloat duration = 1.0f;
    CGFloat itemInterspace = lineSize.width*3;
    
    NSMutableArray *beginTimes = [NSMutableArray array];
    for (int index = 0; index < 20; index++) {
        [beginTimes addObject:@((20-index)/8.0)];
    }
    
    CAMediaTimingFunction *timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.2f :0.68f :0.18f :1.08f];
    
    // Animation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
    animation.removedOnCompletion = NO;
    
    animation.keyTimes = @[@0.0f, @0.5f, @1.0f];
    animation.values = @[@1.0f, @0.1f, @1.0f];
    animation.timingFunctions = @[timingFunction, timingFunction];
    animation.repeatCount = HUGE_VALF;
    animation.duration = duration;
    
    for (int i = 0; i < beginTimes.count; i++) {
        CAShapeLayer *line = [CAShapeLayer layer];
        UIBezierPath *linePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, lineSize.width, lineSize.height) cornerRadius:lineSize.width / 2];
        
        animation.beginTime = [beginTimes[i] floatValue];
        line.fillColor = tintColor.CGColor;
        line.path = linePath.CGPath;
        [line addAnimation:animation forKey:@"animation"];
        line.frame = CGRectMake(itemInterspace * i, 0, lineSize.width, lineSize.height);
        [layer addSublayer:line];
    }
    
    return layer;
}

@end

/*
 参考文献：
 https://www.jianshu.com/p/8e72b2e18166
 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW7
 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/CoreAnimationBasics/CoreAnimationBasics.html#//apple_ref/doc/uid/TP40004514-CH2-SW12
 1.anchorPoint：左上角是(0,0)点
*/
